
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.report;

import java.sql.ResultSet;
import java.util.Hashtable;

import logger.PoolLogger;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import display.BuildFile;
import display.WriteTOTextFile;

public class ReportManager extends Thread 
{
	//DBManager database;
	int Status=5;
	GetIdName gin;
	String object;
	String buffer[];
	String TYPE="type";
	final String REPORT = "report";
	final String PRINTREPORT = "printreport";
	PoolLogger pl;
	
	public ReportManager() 
	{
		gin = new GetIdName();
		TYPE=gin.getId(TYPE);
		
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());

	}
	public void initializeData() 
	{
	}
	
	public void setobject(String object) 
	{
		this.object = object;
	}
	
	public void run() 
	{
		boolean flag=true;
		while(true)
		{
			try 
			{	
				
				ResultSet rs = DBManager.getSelect("select tid,process,report_name,reporton from report_master where status < "+Status + " order by id");
				do
				{
					rs.next();
					String value = rs.getString("tid");
					String process = rs.getString("process");
					String report_name=rs.getString("report_name");
					String Reportid =  gin.getId(report_name);
					String type  = getType(Reportid);
					String reporton = gin.getItem(rs.getString("reporton"));
					if(type.equalsIgnoreCase(REPORT))
					{
						Report report = new Report();
						report.setValue(value);
						report.initPool();
						report.setProcess(process);
						report.setReportName(report_name);
						report.setObject(Reportid);
						report.setrepoton(reporton);
						report.initializeData();
						if(process.toLowerCase().equals("add"))
							report.addReport();
						else 
							report.updateReport();
						DBManager.getUpdate("update report_master set status=5 where tid='"+value+"' and report_name='"+report_name+"'");
					}	
					else if(type.equalsIgnoreCase(PRINTREPORT))
					{
						BuildFile buildfile = new BuildFile(report_name,value);
						buildfile.run();
						WriteTOTextFile writetotextfile =new WriteTOTextFile(report_name);
						writetotextfile.WriteToTextFile(value);
						DBManager.getUpdate("update report_master set status=5 where tid='"+value+"' and report_name='"+report_name+"'");
					}
				
					
				}while(true);
				//
			}
			catch (Exception e) 
			{
				try 
				{
					this.sleep(100);
				} 
				catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}
		}
		 
	}

	public static void main(String[] args) 
	{
		ReportManager rm = new ReportManager();
		rm.start();
	}
	
	
	public String getType(String reportid)
	{
		String type ="";
		RecordSearch sr = new RecordSearch();
		Hashtable<Object, Object> table =new Hashtable<Object, Object>();
		table.put("mid", reportid);
		table.put("pid", TYPE);
		table.put("td", "null");
		sr.setConditionMap(table);
		String result[] = sr.getArrayFromResultSet();
		for(int i=0;i<result.length;i++)
		{
			String splitresult[] = result[i].split("\t");
			type = splitresult[Column.pv_index-1];
			if(type.equals("0"))
				type = splitresult[Column.vt_index-1];
			else
				type  = gin.getItem(type);
			
		}
		return type;
	}
}
